package com.erps.config;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSON;
import com.erps.CommonUtils.ActionResult;
import com.erps.CommonUtils.AppConfig;
import com.erps.CommonUtils.DateUtils;
import com.erps.CommonUtils.JActiveTable;
import com.erps.CommonUtils.Sequence;
import com.erps.CommonUtils.RequestUtils;
import com.erps.CommonUtils.StringEx;
import com.erps.CommonUtils.UploadInfo;
import com.erps.CommonUtils.UploadPostFiles;
import com.erps.CommonUtils.ValueType;
import com.erps.dao.ErspUserFavDao;
import com.erps.db.DBManager;
import com.hmkj.entity.ErspResHuman;
import com.hmkj.entity.ErspUserFav;
import com.hmkj.entity.vo.ersp_res_info_vo;
import com.jfinal.core.Controller;

public class ServicesRestController extends Controller {

	public void dofav() {
		ErspUserFavDao dao = new ErspUserFavDao();
		ErspUserFav vo = new ErspUserFav();
		vo.setUser_id(StringEx.getStr(getSessionAttr(AppConfig.SESSION_USERGUID)));
		vo.setRes_id(this.getPara("res_id"));
		vo.setFav_order(0);
		int iCode = dao.add(vo);
		ActionResult ret;
		if (iCode > 0)
			ret = ActionResult.Valid(ValueType.SUCCESS);
		else
			ret = ActionResult.Valid(ValueType.FAILURE);
		renderJson(JSON.toJSONString(ret));
	}

	public void other_save() {
		ersp_res_info_vo voMaster = new ersp_res_info_vo();
		HttpServletRequest request = this.getRequest();
		ErspResHuman voSlave = new ErspResHuman();

		UploadPostFiles vUpload = new UploadPostFiles(this.getRequest(), this.getResponse());
		JActiveTable aMaster = RequestUtils.getFromRequest(voMaster, request);
		aMaster.setDBTableName("ersp_res_info");
		String cKeyID = Sequence.getGuid();
		aMaster.setFieldValue("id", cKeyID);
		aMaster.setFieldValue("last_update_time", String.valueOf(DateUtils.getDayTimeNum()));
		aMaster.setFieldValue("org_id", StringEx.getStr(getSessionAttr(AppConfig.SESSION_ORGID)));

		JActiveTable aSlave = RequestUtils.getFromRequest(voSlave, request);
		aSlave.setDBTableName("ersp_res_human");
		aSlave.setFieldValue("res_id", cKeyID);
		aSlave.setFieldValue("id", Sequence.getGuid());
		aSlave.setFieldValue("last_update_time", String.valueOf(DateUtils.getDayTimeNum()));

		List<UploadInfo> vUploadList = vUpload.SavePostFile(cKeyID);
		List<String> FileSQLs = vUpload.getUploadSQL(vUploadList);

		String[] sqls = new String[2 + FileSQLs.size()];
		sqls[0] = aMaster.getParmSQL();
		List<Object[]> ParmList = new ArrayList<Object[]>();
		ParmList.add(aMaster.getParmList());

		sqls[1] = aSlave.getParmSQL();
		ParmList.add(aSlave.getParmList());

		for (int i = 0; i < FileSQLs.size(); i++) {
			sqls[2 + i] = FileSQLs.get(i);
			ParmList.add(null);
		}

		int iCode = DBManager.ExecSQL(sqls, ParmList);
		ActionResult ret;

		if (iCode > 0)
			ret = ActionResult.Valid(ValueType.SUCCESS);
		else
			ret = ActionResult.Valid(ValueType.FAILURE);
		renderJson(JSON.toJSONString(ret));
	}
}
